package com.deer.wms.ware.task.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.constant.InventoryConstant;
import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.StringUtils;
import com.deer.wms.ware.task.dao.OutDetailMapper;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.out.OutDetail;
import com.deer.wms.ware.task.model.out.OutMaster;
import com.deer.wms.ware.task.service.OutDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 发运单detail 服务实现类
 * </p>
 *
 * @author luowei
 * @since 2022-04-19 09:07:42
 */
@Service
public class OutDetailServiceImpl extends SuperServiceImpl<OutDetailMapper, OutDetail> implements OutDetailService {

    @Autowired
    private InventoryTransactService inventoryTransactService;

    @Override
    public List<InventoryTransact> toStockRequest(OutMaster outMaster, CurrentUser currentUser) {
        List<OutDetail> outDetails = this.list(new QueryWrapper<OutDetail>().eq("bill_no", outMaster.getBillNo()));
        List<InventoryTransact> invTrans = new ArrayList<>();

        for (OutDetail outDetail : outDetails) {
            InventoryTransact inventoryTransact = new InventoryTransact();
            //Step1.准备数据
            //仓库
            inventoryTransact.setWareId(outMaster.getWareId());
            inventoryTransact.setWareName(outMaster.getWareName());
            //货主
            inventoryTransact.setFromOrganizationId(outMaster.getOrganizationId());
            inventoryTransact.setFromOrganizationName(outMaster.getOrganizationName());
            inventoryTransact.setToOrganizationId(outMaster.getOrganizationId());
            inventoryTransact.setToOrganizationName(outMaster.getOrganizationName());
            //库位
            inventoryTransact.setFromCellCode(outDetail.getCellCode());
            inventoryTransact.setToCellCode(null);
            //容器
            inventoryTransact.setFromBoxCode(outDetail.getBoxCode());
            inventoryTransact.setToBoxCode(null);
            //物料
            inventoryTransact.setItemCode(outDetail.getItemCode());
            inventoryTransact.setItemName(outDetail.getItemName());
            inventoryTransact.setImgUrl(outDetail.getImgUrl());
            //批次
            inventoryTransact.setFromBatchId(outDetail.getBatchId());
            inventoryTransact.setFromBatchName(outDetail.getBatchName());
            inventoryTransact.setToBatchId(null);
            inventoryTransact.setToBatchName(null);

            //数量
            inventoryTransact.setFromQuantity(outDetail.getDeliveryQuantity());
            //包装 转换率
            inventoryTransact.setFromPackDetailId(outDetail.getPackDetailId());
            inventoryTransact.setFromPackDescribe(outDetail.getPackDescribe());
            inventoryTransact.setFromTransRatio(1.0);
            //创建人 时间
            inventoryTransact.setCreateUserId(currentUser.getUserId());
            inventoryTransact.setCreateUserName(currentUser.getUserName());
            inventoryTransact.setCreateTime(DateUtil.now());
            //事务类型
            inventoryTransact.setTransactType(InventoryConstant.INVENTORY_TYPE_OUT);
            //来源单据号
            inventoryTransact.setBillNo(outDetail.getDetailNo());
            invTrans.add(inventoryTransact);
        }

        //Step2.保存，返回
        inventoryTransactService.saveBatch(invTrans);
        return invTrans;
    }
}
